﻿"use strict";
define(['admin/app', "service/admin/billService", "service/admin/merchantService"], function (app) {
    app.controller("billController", function ($scope, $state, $stateParams, $location, utils, billService, merchantService, w5cValidator) {
        $scope.query = {
            page: 1, size: 20, merchant_id: 0, merchant_no: '', mobile: '', name: '', is_oem: false
        };
        $.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                billService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            get_oems: function () {
                merchantService.getoem().success(function (response) {
                    $scope.oems = response.data;
                    $scope.oems.unshift({ id: 0, name: "全部" });
                });
            },
            goBack: function () {
                history.back();
            },
            search_page: function (page, flag) {
                $scope.query.page = page;
                $location.search('page', $scope.query.page);
            },
            bill_detail: function (id) {
                $scope.query.bill_id = id;
                billService.get($scope.query).success(function (response) {
                    $scope.list = response.data;
                });
            },
            search_detail: function () {
                billService.get($scope.query).success(function (response) {
                    $scope.list = response.data;
                });
            },
            get_pay_type: function (id) {
                var pay_type = new Array("", "扫码支付", "快捷支付", "网关支付", "公众号/服务窗支付", "代付(通道结算到提现账号)", "SDK支付", "代收");
                return pay_type[id];
            },
            recharge: function () {
                $scope.psdisabled = true;
                billService.recharge($scope.model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) {
                        methods.goBack();
                    }
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            }
        }
        angular.extend($scope, methods);
        if ($state.includes('app.bill.detail')) {
            methods.bill_detail($stateParams.id);
        } else if ($state.includes('app.bill.recharge')) {
            $scope.model = {};
            $scope.model.bill_id = $stateParams.id;
        } else {
            methods.get_oems();
            methods.search();
        }
    });
});
